package com.jrzh.db.data.execute.sys;

import java.io.InputStream;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;

import com.jrzh.framework.execute.ExecuteBase;
import com.jrzh.mvc.model.sys.ConfigModel;
import com.jrzh.mvc.service.sys.ConfigServiceI;

public class ExecuteConfig extends ExecuteBase<ConfigModel>{

	private static final long serialVersionUID = 4845429107863341289L;
	
	/*private InputStream getInitFile(String name, String module){
		InputStream in = null;
	try {
			File file =null;
			URL url =this.getClass().getResource("/com/jrzh/db/data/json/"+module+"/"+name+".json");
			if(url!=null){
				file =new File(url.getFile());
				if(file.exists()){
					in = new FileInputStream(file);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return in;
	}*/
	
	@Override
	public void execute(ApplicationContext context) {
		try {
			//SysConfigration sysConfigration = (SysConfigration)context.getBean("sysConfigration");
			String project = "sys";
			InputStream in = getInputStreamByName("config", project);
			if(null == in){
				in=	getInitFile("config",project);
			}
			if(null != in){
				ConfigServiceI configService = (ConfigServiceI)context.getBean("configService");
				configService.saveInit(project, in);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Boolean equals(ConfigModel source, ConfigModel target) {
		return StringUtils.equals(source.getGroup(), target.getGroup()) &&
				   StringUtils.equals(source.getName(), target.getName()) &&
				   StringUtils.equals(source.getValue(), target.getValue()) &&
				   StringUtils.equals(source.getDataType(), target.getDataType());
	}

}
